home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
presto
/
presto10.lha
/
src
/
misc.C
< prev
next >
Wrap
C/C++ Source or Header
|
1991-12-11
|
2KB
|
136 lines
/*
* misc.c
*
* General mishmash that doesn't belong anywhere else
* - fatalerror() -- abort and die horribly
* - overloaded new and delete
*/
#define _MISC_C
#include <stream.h>
#include <osfcn.h>
#include "presto.h"
#include <stdio.h>
#ifdef __DECCXX
#define malloc_t void*
#define malloc_arg size_t
#else
#define malloc_t char*
#define malloc_arg unsigned
#endif
void
error(char *s)
{
cerr << s << "\n";
fatalerror();
}
void
fatalerror()
{
cerr << "Aborting....\n";
cout.flush();
cerr.flush();
abort();
}
//
// We have to redefine the new and delete functions so they malloc
// in shared memory.
//
typedef void (*PFVV)();
extern PFVV _new_handler;
typedef malloc_t (*PFUC)(malloc_arg); // for malloc
typedef void (*PFVC)(malloc_t); // for free
//
// The memory allocator should be called "malloc" in all
// Presto versions. That prevents the c-library malloc from being
// linked in and called accidentally. A call to the c-library
// malloc will cause Topaz Presto to get blown out of the water.
//
#define MALLOC(x) malloc(x)
#define FREE(x) free(x)
PFUC mallocf = malloc;
PFVC freef = (PFVC) free;
extern void* operator new(size_t size)
{
malloc_t p;
while ( (p=MALLOC((malloc_arg) size))==0 ) {
#ifndef __DECCXX
if(_new_handler)
(*_new_handler)();
else
#endif /* __DECCXX */
return 0;
}
return (void*)p;
}
extern void operator delete(void* p)
{
if (p) FREE((malloc_t) p);
}
//
// Dangerous: If you do a set_{malloc/free}_agent from shared to non-shared
// or the other way around, anything which has been malloced in one
// form might get demalloced in another. Of course, the classes
// can remember this for themselves and make sure to set the
// types correctly.
PFUC
set_malloc_agent(PFUC nmalloc)
{
PFUC omalloc = mallocf;
mallocf = nmalloc;
return omalloc;
}
PFVC
set_free_agents(PFVC nfree)
{
PFVC ofree = freef;
freef = nfree;
return ofree;
}
void failed_malloc()
{
write (1, "failed malloc\n", 14);
// cerr << "operator new failed: out of store\n";
fatalerror();
}
void
thisthread_holdingspinlock()
{
thisthread->holdingspinlock();
}
void
thisthread_releasingspinlock()
{
thisthread->releasingspinlock();
}
#if defined(sun) || defined(vax) || defined(mips)
int cpus_online() { return 1; }
malloc_t shmalloc(size_t s) { return malloc(s); }
void shfree(char* x) { free(x); }
#endif /* sun||vax||mips */